package pub.cleangao.codegen.sqlexe;

import lombok.Getter;

@Getter
public enum DbType {
    MARIADB(1,
            "MariaDB",
            "org.mariadb.jdbc.Driver",
            "jdbc:mariadb://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true",
            "mariadb"
    ),
    MYSQL(2,
            "MySQL",
            "com.mysql.cj.jdbc.Driver",
            "jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true",
            "mysql"
    ),
    ORACLE(4,
            "Oracle",
            "oracle.jdbc.driver.OracleDriver",
            "jdbc:oracle:thin:@%s:%s/%s",
            "oracle"),
    SQL_SERVER(8,
            "SQL Server",
            "com.microsoft.sqlserver.jdbc.SQLServerDriver",
            "jdbc:sqlserver://%s:%s;DatabaseName=%s",
            "sqlserver"),
    POSTGRE_SQL(16,
            "PostgreSQL",
            "org.postgresql.Driver",
            "jdbc:postgresql://%s:%s/%s",
            "postgresql"),
    SQL_ITE(32,
            "SQLite",
            "org.sqlite.JDBC",
            "jdbc:sqlite:%s",
            "sqlite"),

    ;
    private final Integer type;
    private final String displayName;
    private final String driverClass;
    private final String jdbcUrl;
    private final String signature;

    DbType(int type, String displayName, String driverClass, String jdbcUrl, String signature) {
        this.type = type;
        this.displayName = displayName;
        this.driverClass = driverClass;
        this.jdbcUrl = jdbcUrl;
        this.signature = signature;
    }


    public static DbType of(String signature) {
        for (DbType value : DbType.values()) {
            if (value.signature.equals(signature)) {
                return value;
            }
        }
        return null;
    }

    public static DbType of(Integer type) {
        for (DbType value : DbType.values()) {
            if (value.type.equals(type)) {
                return value;
            }
        }
        return null;
    }

    public static DbType fromDriverClass(String driverClass) {
        for (DbType dbType : DbType.values()) {
            if (dbType.getDriverClass().equals(driverClass)) {
                return dbType;
            }
        }
        return null;
    }


}
